﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Trifecta.Models;

namespace Trifecta.Controllers
{
    public class AgentMerchantController : Controller
    {
        private AndPOSEntities db = new AndPOSEntities();

        // GET: /AgentMerchant/
        public ActionResult Index()
        {
            var agent_merchant = db.Agent_Merchant.Include(a => a.Agent).Include(a => a.Merchant);
            return View(agent_merchant.ToList());
        }

        // GET: /AgentMerchant/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Agent_Merchant agent_merchant = db.Agent_Merchant.Find(id);
            if (agent_merchant == null)
            {
                return HttpNotFound();
            }
            return View(agent_merchant);
        }

        // GET: /AgentMerchant/Create
        public ActionResult Create()
        {
            ViewBag.Agent_ID = new SelectList(db.Agents, "Agent_ID", "First_Name");
            ViewBag.Merchant_ID = new SelectList(db.Merchants, "Merchant_ID", "Merchant_Name");
            return View();
        }

        // POST: /AgentMerchant/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="AgentMerchant_ID,Agent_ID,Merchant_ID,Activate_Date,Retired_Date,Memo")] Agent_Merchant agent_merchant)
        {
            if (ModelState.IsValid)
            {
                db.Agent_Merchant.Add(agent_merchant);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Agent_ID = new SelectList(db.Agents, "Agent_ID", "First_Name", agent_merchant.Agent_ID);
            ViewBag.Merchant_ID = new SelectList(db.Merchants, "Merchant_ID", "Merchant_Name", agent_merchant.Merchant_ID);
            return View(agent_merchant);
        }

        // GET: /AgentMerchant/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Agent_Merchant agent_merchant = db.Agent_Merchant.Find(id);
            if (agent_merchant == null)
            {
                return HttpNotFound();
            }
            ViewBag.Agent_ID = new SelectList(db.Agents, "Agent_ID", "First_Name", agent_merchant.Agent_ID);
            ViewBag.Merchant_ID = new SelectList(db.Merchants, "Merchant_ID", "Merchant_Name", agent_merchant.Merchant_ID);
            return View(agent_merchant);
        }

        // POST: /AgentMerchant/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="AgentMerchant_ID,Agent_ID,Merchant_ID,Activate_Date,Retired_Date,Memo")] Agent_Merchant agent_merchant)
        {
            if (ModelState.IsValid)
            {
                db.Entry(agent_merchant).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Agent_ID = new SelectList(db.Agents, "Agent_ID", "First_Name", agent_merchant.Agent_ID);
            ViewBag.Merchant_ID = new SelectList(db.Merchants, "Merchant_ID", "Merchant_Name", agent_merchant.Merchant_ID);
            return View(agent_merchant);
        }

        // GET: /AgentMerchant/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Agent_Merchant agent_merchant = db.Agent_Merchant.Find(id);
            if (agent_merchant == null)
            {
                return HttpNotFound();
            }
            return View(agent_merchant);
        }

        // POST: /AgentMerchant/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Agent_Merchant agent_merchant = db.Agent_Merchant.Find(id);
            db.Agent_Merchant.Remove(agent_merchant);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
